1 00:00:00,320 --> 00:00:00,890 All right. 2 00:00:00,890 --> 00:00:06,770 So in this lecture I think we can continue to improve the ambience in our game by scripting the animations 3 00:00:06,770 --> 00:00:10,760 for our Squidward friend here that's going to be sitting in the front of the kitchen. 4 00:00:10,760 --> 00:00:15,230 We're basically going to be doing the exact same thing that we did in the lobby, where we'll have a 5 00:00:15,230 --> 00:00:20,330 default animation or idle animation for our Squidward, and then we'll play a slightly different idle 6 00:00:20,330 --> 00:00:21,770 animation randomly. 7 00:00:21,830 --> 00:00:27,680 So make sure inside of your regular Squidward that you have uploaded all of the different idling animations 8 00:00:27,680 --> 00:00:34,400 that I've saved in there, and that you've placed them inside of their respective idle animation instances. 9 00:00:34,400 --> 00:00:39,920 Because then what we could do now is we could go into server script service inside of server, and we 10 00:00:39,920 --> 00:00:42,950 can go ahead and go to our normal Squidward service. 11 00:00:43,070 --> 00:00:46,670 Inside of here, we're going to make a reference to two services. 12 00:00:46,670 --> 00:00:48,800 One is going to be server storage. 13 00:00:51,700 --> 00:00:55,540 And then we're also going to have a reference to the server script service. 14 00:00:59,990 --> 00:01:02,990 And then we can create our variable that will represent the service. 15 00:01:02,990 --> 00:01:07,370 So normal Squidward service is equal to a table. 16 00:01:07,370 --> 00:01:10,010 And then we'll return that at the end of our script here. 17 00:01:11,660 --> 00:01:14,870 And now we can make a reference to our regular Squidward. 18 00:01:14,870 --> 00:01:21,470 So he is inside of the workspace in the characters folder, and then we can get our regular Squidward. 19 00:01:21,470 --> 00:01:27,860 We want to make a reference to his humanoid so regular Squidward dot humanoid, and then we can go ahead 20 00:01:27,860 --> 00:01:29,450 and get his animator. 21 00:01:29,450 --> 00:01:36,560 So animator is equal to humanoid dot animator, and then we can make a reference to his idle animation 22 00:01:36,560 --> 00:01:37,100 regular. 23 00:01:37,100 --> 00:01:43,250 So that's in regular Squidward, Dot, idle and M1, and then we'll collect all of the other idle animations 24 00:01:43,250 --> 00:01:49,220 that he has, so we'll call idle animes regular Squidward dot idle animes. 25 00:01:49,990 --> 00:01:51,910 And then we'll get all those children. 26 00:01:52,780 --> 00:01:56,770 And lastly, we're also going to need a random data type. 27 00:01:58,120 --> 00:02:04,030 Now I'm also going to be making a reference to our binding event that's in server storage. 28 00:02:04,030 --> 00:02:10,090 So we're going to refer to our game comms event for the server scripts inside of server storage dot 29 00:02:10,090 --> 00:02:12,460 events dot Bindable dot game comms. 30 00:02:12,460 --> 00:02:17,500 Because we're going to be using this event for when we need to move our Squidward. 31 00:02:17,500 --> 00:02:19,090 So we're going to have him later. 32 00:02:19,090 --> 00:02:22,180 Walk to a point right here that's invisible point. 33 00:02:22,180 --> 00:02:26,920 And then he's going to open the store, walk down through here to this second point, open this door, 34 00:02:26,920 --> 00:02:28,600 and then enter into the basement. 35 00:02:28,600 --> 00:02:32,110 So this is going to be the part in our story where the lights go out. 36 00:02:32,110 --> 00:02:35,110 And then he's going to go check out the basement or the breaker in the basement. 37 00:02:35,110 --> 00:02:37,540 And then he disappears and we have to go look for him. 38 00:02:37,540 --> 00:02:42,550 So we're going to be using that event later when we need to go ahead and move our Squidward. 39 00:02:42,970 --> 00:02:48,130 And because we're going to be doing that, we also need to make a reference to our game comms enum for 40 00:02:48,130 --> 00:02:56,770 the server, so we can require from server script service, dot server, dot modules, dot enums, dot 41 00:02:56,770 --> 00:02:58,540 game communication enum. 42 00:02:59,370 --> 00:03:04,050 Now, since we're going to be doing the exact same thing that we did in our lobby, we of course need 43 00:03:04,050 --> 00:03:07,410 a function for starting the idle animations. 44 00:03:07,710 --> 00:03:14,580 We're going to need a function for getting a random track or idle animation track, and that means we're 45 00:03:14,580 --> 00:03:21,360 actually going to have to make sure to load all of our idle animations onto our, uh, humanoid. 46 00:03:21,360 --> 00:03:29,310 So for every single animation in idle animation, what we need to do is we need to get the track from 47 00:03:29,310 --> 00:03:36,360 our animator load animation anim, and then we need to take that track and insert it into a table so 48 00:03:36,360 --> 00:03:45,000 we can have idle anim tracks, and then we can insert this track into our idle anim tracks table. 49 00:03:45,000 --> 00:03:47,250 And then we'll pass the track perfect. 50 00:03:47,250 --> 00:03:53,460 And then inside of our get random track function, all we need to do is return from our Idle Anim Tracks 51 00:03:53,460 --> 00:03:54,270 table. 52 00:03:54,270 --> 00:04:00,090 We'll use our RNG next integer function, pass one and then the max number of idle anim tracks. 53 00:04:00,090 --> 00:04:02,040 So we just get a random animation. 54 00:04:02,040 --> 00:04:08,070 We also need to make sure to load our regular default idle animation right here, so we can create a 55 00:04:08,070 --> 00:04:08,970 variable for that real quick. 56 00:04:08,970 --> 00:04:16,290 We'll call it idle anim track is equal to the animator load animation idle anim regular. 57 00:04:16,290 --> 00:04:22,860 And then from this point inside of our star idle animation function, what we could do is we could listen 58 00:04:22,860 --> 00:04:28,290 for while our regular Squidward dot parent is not equal to nil. 59 00:04:28,410 --> 00:04:34,710 And this is because we're going to have him move to those different positions on our map later on. 60 00:04:34,710 --> 00:04:37,020 And when that happens, he'll reach this position. 61 00:04:37,020 --> 00:04:40,860 Open this door, open this door, reach the second one, and then go to the third position. 62 00:04:40,860 --> 00:04:44,790 And after that, we're just going to delete them off the map because we don't need them anymore. 63 00:04:44,790 --> 00:04:49,170 And when he gets deleted off of the map, that means his parent is going to be set to nil. 64 00:04:49,170 --> 00:04:53,880 So when his parent does get set to nil, we want to stop looping and animating him. 65 00:04:53,880 --> 00:05:00,810 So in this loop, we can check if our idle animation track is not playing. 66 00:05:00,810 --> 00:05:02,820 So if it's not playing then we need to play it. 67 00:05:02,820 --> 00:05:05,850 So idle anim track play. 68 00:05:06,820 --> 00:05:11,620 And then this idle animation track has that exact same event inside of it. 69 00:05:11,620 --> 00:05:17,290 So idle anim track, we're going to get marker reach signal, which is the ended event, and we're going 70 00:05:17,290 --> 00:05:18,220 to wait for this. 71 00:05:18,220 --> 00:05:23,440 And when that is reached we can use our RNG function next integer between 1 and 8. 72 00:05:23,440 --> 00:05:25,720 So a one out of eight chance. 73 00:05:26,420 --> 00:05:31,100 And what we could do is we could get a track from our get random track function. 74 00:05:31,100 --> 00:05:35,720 We'll play the track and then we'll wait for the track to complete. 75 00:05:37,540 --> 00:05:43,180 Another function we're going to need is to listen to our game comms event, so we can call it on Game 76 00:05:43,180 --> 00:05:44,860 comms event. 77 00:05:45,310 --> 00:05:49,120 We'll get passed in action and arguments for that action. 78 00:05:49,750 --> 00:05:54,190 And that means we're going to have to fill out our game comms enum on the server for our particular 79 00:05:54,190 --> 00:05:54,940 service here. 80 00:05:54,940 --> 00:06:01,030 So inside of server script service, inside of the modules and enum section, we can open this up and 81 00:06:01,030 --> 00:06:04,720 we can create a key for our normal Squidward. 82 00:06:06,330 --> 00:06:11,820 Service, and the only action that we want to have in here is basically just to move our Squidward to 83 00:06:11,820 --> 00:06:16,920 a different position, because later on, again in our story, we're going to have our Squidward walking 84 00:06:16,920 --> 00:06:21,750 along those invisible points when he goes and disappears into the basement and the player's got to go 85 00:06:21,750 --> 00:06:22,560 look for him. 86 00:06:22,560 --> 00:06:26,460 So that's the only action that we'll need for our normal Squidward service. 87 00:06:26,460 --> 00:06:32,040 So that means inside of our on Gamescom event function, we can listen for if the action is equal to 88 00:06:32,040 --> 00:06:37,410 the game comms enum server dot normal Squidward service dot move Squidward. 89 00:06:37,410 --> 00:06:40,020 And if it is, then we can have a function for moving him. 90 00:06:40,020 --> 00:06:41,730 So let's make that real quick. 91 00:06:41,940 --> 00:06:49,710 We'll call this function Move Squidward to positions, and we'll pass the arguments from this function 92 00:06:49,710 --> 00:06:53,190 to this function, and we can just call it right here and pass the args. 93 00:06:53,190 --> 00:06:57,150 We don't need to fill this function out right now, but we will have to fill it out later. 94 00:06:57,330 --> 00:07:02,550 Now we can go ahead and fill out the initialize and start functions for this service. 95 00:07:02,550 --> 00:07:03,390 So normal. 96 00:07:03,390 --> 00:07:06,240 Squidward service init. 97 00:07:07,000 --> 00:07:13,540 And inside of this function, all we need to do is to connect our function to the game comms event. 98 00:07:13,540 --> 00:07:17,620 So event connect on Gamescom event. 99 00:07:18,370 --> 00:07:28,870 And then for our normal Squidward service Start function, we can call our, uh, start idle animation 100 00:07:28,870 --> 00:07:32,800 and we can spawn this in a new thread using task dot defer. 101 00:07:34,670 --> 00:07:38,300 That way we aren't stuck looping inside of this function forever. 102 00:07:38,800 --> 00:07:43,960 And I'm pretty sure that's all we need to do for our Squidward service for now, so we can go ahead 103 00:07:43,960 --> 00:07:49,480 and test out whether or not our Squidward has animations so we can actually just hit run here. 104 00:07:50,800 --> 00:07:52,660 And the server initializes everything. 105 00:07:52,660 --> 00:07:53,230 And there we go. 106 00:07:53,230 --> 00:07:57,070 We have our Squidward Fran right here, all nice and animated. 107 00:07:57,070 --> 00:07:58,120 Just chilling out. 108 00:07:58,690 --> 00:08:03,040 Let's see if we can actually get another one of his idle animations to play here real quick. 109 00:08:04,480 --> 00:08:05,410 And there we go. 110 00:08:05,410 --> 00:08:08,110 Another one of his random idling animations has played. 111 00:08:08,740 --> 00:08:15,400 So now we have further improved the ambience in our game, and so far our game is looking pretty good. 112 00:08:15,550 --> 00:08:18,490 Keep it up and I will see you in the next lecture.